package com.ellricky.baselib.widget.calendar;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.andr.common.util.date.TimeUtil;
import com.ellricky.baselib.R;

import java.util.ArrayList;
import java.util.Calendar;

/**
 * author：Administrator on 2017/4/10 16:04
 * description:文件说明
 * version:版本
 */
public class WeekDateView extends LinearLayout implements View.OnClickListener, AdapterView.OnItemClickListener {
    private ArrayList<WeekDateEntity> curWeekList;

    private TextView tvYear;
    private TextView tvFrontWeek;
    private TextView tvNextWeek;
    private TextView tvWeekRange;
    private RecyclerView weekRv;

    private WeekDateAdapter mAdapter;
    private String dataFormate = "yyyy-MM-dd";
    private String currentDate;

    public WeekDateView(Context context) {
        this(context, null);
    }

    public WeekDateView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WeekDateView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        View.inflate(context, R.layout.base_layout_week_date, this);

        curWeekList = new ArrayList<>();

        tvYear = findViewById(R.id.tvYear);
        int year = TimeUtil.getFieldValue(null, Calendar.YEAR);
        tvYear.setText(String.valueOf(year));

        tvFrontWeek = findViewById(R.id.tvFrontWeek);
        tvNextWeek = findViewById(R.id.tvNextWeek);
        tvWeekRange = findViewById(R.id.tvWeekDate);

        weekRv = findViewById(R.id.rvWeek);

        tvFrontWeek.setOnClickListener(this);
        tvNextWeek.setOnClickListener(this);

        GridLayoutManager glm = new GridLayoutManager(context, 7) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };

        weekRv.setLayoutManager(glm);
        weekRv.setNestedScrollingEnabled(false);

        mAdapter = new WeekDateAdapter(getContext());
        weekRv.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(this);
    }

    public void getData(String dateNumber,boolean changeWeek) {
        curWeekList.clear();

        if (TextUtils.isEmpty(dateNumber)) {
            dateNumber = DateUtils.getCurrDate(dataFormate);
        }

        curWeekList = DateUtils.getWeek(dateNumber);
        if (curWeekList == null || curWeekList.size() <= 0) {
            return;
        }

        String weekStartDate = curWeekList.get(0).getDate();
        String[] sArr = weekStartDate.split("-");
        weekStartDate = sArr[1] + "月" + sArr[2] + "日";

        String weekEndDate = curWeekList.get(curWeekList.size() - 1).getDate();
        String[] eArr = weekEndDate.split("-");
        weekEndDate = eArr[1] + "月" + eArr[2] + "日";

        tvWeekRange.setText(String.format("%s～%s", weekStartDate, weekEndDate));
        mAdapter.refresh(curWeekList);

        for (int i = 0; i < curWeekList.size(); i++) {
            if (dateNumber.equals(curWeekList.get(i).getDate())) {
                mAdapter.setSelectedPosition(i);
                currentDate = curWeekList.get(i).getDate();
                if (onSelectListener != null) {
                    onSelectListener.onSelected(i, curWeekList.get(i));
                    if (changeWeek) onSelectListener.onChangeWeek(i);
                }
            }
        }

        if (TextUtils.isEmpty(currentDate)) {
            mAdapter.setSelectedPosition(0);
            currentDate = curWeekList.get(0).getDate();
            if (onSelectListener != null) {
                onSelectListener.onSelected(0, curWeekList.get(0));
            }
        }
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == tvFrontWeek.getId()) {
            getData(DateUtils.getSomeDays(currentDate, -7),true);
        } else if (id == tvNextWeek.getId()) {
            getData(DateUtils.getSomeDays(currentDate, +7),true);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        WeekDateEntity entity = mAdapter.get(position);
        currentDate = entity.getDate();
        mAdapter.setSelectedPosition(position);
        if (onSelectListener != null) {
            onSelectListener.onSelected(position, curWeekList.get(position));
        }
    }


    /**
     * 选中日期之后的回调
     */
    private OnSelectListener onSelectListener;

    public void setOnSelectListener(OnSelectListener onSelectListener) {
        this.onSelectListener = onSelectListener;
    }

    public interface OnSelectListener {
        void onSelected(int position, WeekDateEntity dateEntity);
        void onChangeWeek(int position);
    }
}
